﻿/*HandBrake Batch Encoder Mk II 
 * v2.010 5/16/12 
 * Steven Hawkes
 *
 * This class is a dialog that allows the user to enter a new preset name.   It contains a public property that
 * allows the user to return the new preset name after the dialog closes.
 * 
 * It provides some default validation i.e. that the user has actually entered a name and that the name is unique.
 * 
 */

using System;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using HandBrake_Encoder.Properties;
using log4net;

namespace HandBrake_Encoder
{
    public partial class SavePreset : Form
    {
        public static ILog Log = LogManager.GetLogger("SavePreset");

        public SavePreset()
        {
            // Assign the curent language
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Language);
            InitializeComponent();
        }

        public string NewName { get; set; }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Log.Debug("The user has selected to save the new preset");
            bool status = true;
            DialogResult = DialogResult.OK;
            var presets = new Presets();

            // Check the user has entered a name
            if (newNameTextBox.Text.Length == 0)
            {
                Log.Debug("OK Selected without a name being entered in the Save Preset dialog");
                MessageBox.Show("Please enter a valid name to save this preset");
                DialogResult = DialogResult.Cancel;
                status = false;
            }

            // Check entered name is not a default preset which cannot be modified
            if (status)
            {
                Preset preset = presets.FindPresetByName(newNameTextBox.Text);
                if (preset != null)
                {
                    Presets.PresetType presetType = presets.GetPresetType(preset.Name);
                    if (presetType.Equals(Presets.PresetType.Default))
                    {
                        status = false;
                        DialogResult = DialogResult.Cancel;
                        
                        Log.Info("This name is already used as a default preset and cannot be modified. Please re enter a different name");
                        MessageBox.Show("This name is already used as a default preset and cannot be modified. Please re enter a different name");
                    }
                }
            }

            if (status)
            {
                NewName = newNameTextBox.Text;
                Close();
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Log.Debug("The user has decided to cancel the Save Preset operation");
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void SavePreset_Shown(object sender, EventArgs e)
        {
            newNameTextBox.Focus();
        }
    }
}